package main

import (
	"fmt"
	"sort"
)

//#link: https://leetcode-cn.com/problems/minimize-maximum-pair-sum-in-array/
func main() {
	nums := []int{3, 5, 4, 2, 4, 6}
	fmt.Println(minPairSum(nums))
	nums = []int{4, 1, 5, 1, 2, 5, 1, 5, 5, 4}
	fmt.Println(minPairSum(nums))
	nums = []int{3, 5, 2, 3}
	fmt.Println(minPairSum(nums))
	nums = []int{5, 3, 5, 2, 1, 5, 5, 2, 3, 1}
	fmt.Println(minPairSum(nums))
}

func minPairSum(nums []int) int {
	result := 0
	//1.先排序
	sort.Ints(nums)
	//2直接返回最小的加最大的
	for i := 0; i < len(nums)>>1; i++ {
		result = max(result, nums[i]+nums[len(nums)-i-1])
	}
	return result
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
